package org.jeecg.modules.business.delivery.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.basic.material.service.IBasicMaterialService;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTask;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTaskMaterial;
import org.jeecg.modules.business.delivery.entity.vo.*;
import org.jeecg.modules.business.delivery.mapper.WmsDeliveryTaskMapper;
import org.jeecg.modules.business.delivery.mapper.WmsDeliveryTaskMaterialMapper;
import org.jeecg.modules.business.delivery.service.IWmsDeliveryTaskService;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.entity.vo.RepertorySimpleVO;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.service.ISapOperateService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.transfer.vo.WmsMoveReceiptDetailVO;
import org.jeecg.modules.business.transfer.vo.WmsMoveReceiptVO;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.dock.entity.dto.response.SapBaseResponseDTO;
import org.jeecg.modules.dock.entity.mds.request.MalledMaterialRackRequestVO;
import org.jeecg.modules.dock.service.MdsDockingRepeaterService;
import org.jeecg.modules.flow.callback.enums.SimpleDocumentTypeEnum;
import org.jeecg.modules.flow.callback.enums.TransferStatusEnum;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 调拨任务
 * @Author: jeecg-boot
 * @Date: 2023-11-22
 * @Version: V1.0
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WmsDeliveryTaskServiceImpl extends ServiceImpl<WmsDeliveryTaskMapper, WmsDeliveryTask> implements IWmsDeliveryTaskService {


    private final WmsDeliveryTaskMapper wmsDeliveryTaskMapper;

    private final WmsDeliveryTaskMaterialMapper wmsDeliveryTaskMaterialMapper;

    private final IWmsOutReceiptTaskService outReceiptTaskService;

    private final IWmsInReceiptTaskService inReceiptTaskService;

    private final IWmsInReceiptTaskMaterialService inReceiptTaskMaterialService;

    private final IWmsOutReceiptTaskMaterialService outReceiptTaskMaterialService;

    private final IWmsInReceiptService inReceiptService;

    private final IWmsOutReceiptService outReceiptService;

    private final IWmsMoveReceiptService moveReceiptService;

    private final IBasicMaterialService materialService;

    private final ISysDictService dictService;

    private final MdsDockingRepeaterService mdsDockingRepeaterService;

    @Lazy
    private final ISapOperateService sapOperateService;

    /**
     * 调拨任务-分页列表查询
     * @param page
     * @param wmsDeliveryTask
     * @return
     */
    @Override
    public IPage<WmsDeliveryTaskPageVO> listByPage(IPage<WmsDeliveryTask> page, WmsDeliveryTaskQueryVO wmsDeliveryTask) {
        return wmsDeliveryTaskMapper.listByPage(page, wmsDeliveryTask);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveMain(WmsDeliveryTask wmsDeliveryTask, List<WmsDeliveryTaskMaterial> wmsDeliveryTaskMaterialList) {
        //设置默认的过账状态
        wmsDeliveryTask.setTransferSapStatus(TransferStatusEnum.NOT_NEED.getCode());
        wmsDeliveryTaskMapper.insert(wmsDeliveryTask);
        if (wmsDeliveryTaskMaterialList != null && wmsDeliveryTaskMaterialList.size() > 0) {
            for (WmsDeliveryTaskMaterial entity : wmsDeliveryTaskMaterialList) {
                //外键设置
                entity.setWmsDeliveryTaskId(wmsDeliveryTask.getId());
                wmsDeliveryTaskMaterialMapper.insert(entity);
            }
        }
        return wmsDeliveryTask.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(WmsDeliveryTask wmsDeliveryTask, List<WmsDeliveryTaskMaterial> wmsDeliveryTaskMaterialList) {
        wmsDeliveryTaskMapper.updateById(wmsDeliveryTask);

        //1.先删除子表数据
        wmsDeliveryTaskMaterialMapper.deleteByMainId(wmsDeliveryTask.getId());

        //2.子表数据重新插入
        if (wmsDeliveryTaskMaterialList != null && wmsDeliveryTaskMaterialList.size() > 0) {
            for (WmsDeliveryTaskMaterial entity : wmsDeliveryTaskMaterialList) {
                //外键设置
                entity.setWmsDeliveryTaskId(wmsDeliveryTask.getId());
                wmsDeliveryTaskMaterialMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        wmsDeliveryTaskMaterialMapper.deleteByMainId(id);
        wmsDeliveryTaskMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            wmsDeliveryTaskMaterialMapper.deleteByMainId(id.toString());
            wmsDeliveryTaskMapper.deleteById(id);
        }
    }

    /**
     * 下查
     *
     * @param taskId
     * @param type
     * @return
     */
    @Override
    public List<DeliveryUnderListVO> underList(String taskId, String type) {

        WmsDeliveryTask deliveryTask = this.getById(taskId);
        if (Objects.isNull(deliveryTask)) {
            throw new JeecgBootException("调拨任务不存在");
        }
        String inTaskCode = deliveryTask.getInTaskCode();
        String outTaskCode = deliveryTask.getOutTaskCode();
        List<DeliveryUnderListVO> underListVOList = new ArrayList<>();

        if (!StrUtil.isEmpty(inTaskCode)) {
            WmsInReceiptTask inReceiptTask = inReceiptTaskService.getOne(new LambdaQueryWrapper<WmsInReceiptTask>()
                    .eq(WmsInReceiptTask::getDocumentCode, inTaskCode)
            );
            if (!Objects.isNull(inReceiptTask)) {
                DeliveryUnderListVO underListVO = new DeliveryUnderListVO();
                underListVO.setDocumentStatus(inReceiptTask.getDocumentStatus());
                underListVO.setDeliveryStatus(null);
                underListVO.setDocumentType(inReceiptTask.getDocumentType());
                underListVO.setDocumentCode(inReceiptTask.getDocumentCode());
                underListVO.setTaskDate(inReceiptTask.getTaskDate());
                underListVO.setDepartmentName(inReceiptTask.getSysOrgCode());
                underListVO.setWarehouseKeeper(inReceiptTask.getWarehouseKeeper());
                underListVO.setCreator(inReceiptTask.getCreateBy());
                underListVO.setCreateTime(inReceiptTask.getCreateTime());
                underListVO.setOrganization(inReceiptTask.getOrganization());
                underListVO.setType(SimpleDocumentTypeEnum.IN_TASK.getCode());

                //设置单据类型
                String taskDocumentType = dictService.queryDictTextByKey("document_task", inReceiptTask.getDocumentType());
                underListVO.setDocumentTypeValue(taskDocumentType);
                underListVOList.add(underListVO);
                List<WmsInReceipt> inReceiptList = inReceiptService.list(new LambdaQueryWrapper<WmsInReceipt>()
                        .eq(WmsInReceipt::getWmsInReceiptTaskId, inReceiptTask.getId())
                );
                if (!CollectionUtil.isEmpty(inReceiptList)) {
                    String receiptDocumentType = dictService.queryDictTextByKey("document_type", inReceiptTask.getDocumentType());
                    for (WmsInReceipt o : inReceiptList) {
                        DeliveryUnderListVO receiptVO = new DeliveryUnderListVO();
                        receiptVO.setDocumentStatus(o.getDocumentStatus());
                        receiptVO.setDeliveryStatus(null);
                        receiptVO.setDocumentType(o.getDocumentType());
                        receiptVO.setDocumentCode(o.getDocumentCode());
                        receiptVO.setTaskDate(o.getCreateTime());
                        receiptVO.setDepartmentName(o.getSysOrgCode());
                        receiptVO.setWarehouseKeeper(o.getWarehouseKeeper());
                        receiptVO.setCreator(o.getCreateBy());
                        receiptVO.setCreateTime(o.getCreateTime());
                        receiptVO.setOrganization(o.getOrganization());
                        receiptVO.setType(SimpleDocumentTypeEnum.IN_RECEIPT.getCode());
                        receiptVO.setDocumentTypeValue(receiptDocumentType);
                        underListVOList.add(receiptVO);
                    }
                }
            }
        }
        if (!StrUtil.isEmpty(outTaskCode)) {
            WmsOutReceiptTask outReceiptTask = outReceiptTaskService.getOne(new LambdaQueryWrapper<WmsOutReceiptTask>()
                    .eq(WmsOutReceiptTask::getDocumentCode, outTaskCode)
            );
            if (!Objects.isNull(outReceiptTask)) {
                DeliveryUnderListVO underListVO = new DeliveryUnderListVO();
                underListVO.setDocumentStatus(outReceiptTask.getDocumentStatus());
                underListVO.setDeliveryStatus(null);
                underListVO.setDocumentType(outReceiptTask.getDocumentType());
                underListVO.setDocumentCode(outReceiptTask.getDocumentCode());
                underListVO.setTaskDate(outReceiptTask.getTaskDate());
                underListVO.setDepartmentName(outReceiptTask.getSysOrgCode());
                underListVO.setWarehouseKeeper(outReceiptTask.getWarehouseKeeper());
                underListVO.setCreator(outReceiptTask.getCreateBy());
                underListVO.setCreateTime(outReceiptTask.getCreateTime());
                underListVO.setOrganization(outReceiptTask.getOrganization());
                underListVO.setType(SimpleDocumentTypeEnum.OUT_TASK.getCode());
                //设置单据类型
                String taskDocumentType = dictService.queryDictTextByKey("outbound_task", outReceiptTask.getDocumentType());
                underListVO.setDocumentTypeValue(taskDocumentType);
                underListVOList.add(underListVO);

                List<WmsOutReceipt> outReceiptList = outReceiptService.list(new LambdaQueryWrapper<WmsOutReceipt>()
                        .eq(WmsOutReceipt::getWmsOutReceiptTaskId, outReceiptTask.getId())
                );
                if (!CollectionUtil.isEmpty(outReceiptList)) {
                    String receiptDocumentType = dictService.queryDictTextByKey("outbound_type", outReceiptTask.getDocumentType());
                    for (WmsOutReceipt o : outReceiptList) {
                        DeliveryUnderListVO receiptVO = new DeliveryUnderListVO();
                        receiptVO.setDocumentStatus(o.getDocumentStatus());
                        receiptVO.setDeliveryStatus(null);
                        receiptVO.setDocumentType(o.getDocumentType());
                        receiptVO.setDocumentCode(o.getDocumentCode());
                        receiptVO.setTaskDate(o.getCreateTime());
                        receiptVO.setDepartmentName(o.getSysOrgCode());
                        receiptVO.setWarehouseKeeper(o.getWarehouseKeeper());
                        receiptVO.setCreator(o.getCreateBy());
                        receiptVO.setCreateTime(o.getCreateTime());
                        receiptVO.setOrganization(o.getOrganization());
                        receiptVO.setType(SimpleDocumentTypeEnum.OUT_RECEIPT.getCode());
                        receiptVO.setDocumentTypeValue(receiptDocumentType);
                        underListVOList.add(receiptVO);
                    }
                }
            }
        }

        List<WmsMoveReceipt> moveReceiptList = moveReceiptService.list(new LambdaQueryWrapper<WmsMoveReceipt>()
                .eq(WmsMoveReceipt::getDeliveryTaskId, taskId)
        );
        if (!CollectionUtil.isEmpty(moveReceiptList)) {
            for (WmsMoveReceipt o : moveReceiptList) {
                DeliveryUnderListVO receiptVO = new DeliveryUnderListVO();
                receiptVO.setDocumentStatus(o.getDocumentStatus());
                receiptVO.setDeliveryStatus(null);
                receiptVO.setDocumentType(o.getDocumentType());
                receiptVO.setDocumentCode(o.getDocumentCode());
                receiptVO.setTaskDate(o.getCreateTime());
                receiptVO.setDepartmentName(o.getSysOrgCode());
                receiptVO.setWarehouseKeeper(o.getWarehouseKeeper());
                receiptVO.setCreator(o.getCreateBy());
                receiptVO.setCreateTime(o.getCreateTime());
                receiptVO.setOrganization(o.getOrganization());
                receiptVO.setType(SimpleDocumentTypeEnum.MOVE_RECEIPT.getCode());
                String receiptDocumentType = dictService.queryDictTextByKey("move_type", o.getDocumentType());
                receiptVO.setDocumentTypeValue(receiptDocumentType);
                underListVOList.add(receiptVO);
            }
        }
        if (!StrUtil.isEmpty(type)) {
            List<DeliveryUnderListVO> collect = underListVOList.stream().filter(o -> type.equals(o.getType())).collect(Collectors.toList());
            return collect;

        } else {
            return underListVOList;
        }
    }

    /**
     * 条料调拨因入移库单-从入库任务中获取移库物料信息
     *
     * @param needMoveMaterialVO
     * @return
     */
    @Override
    public WmsMoveReceiptVO getMoveMaterialFormInTask(NeedMoveMaterialVO needMoveMaterialVO) {
        if (CollectionUtil.isEmpty(needMoveMaterialVO.getTaskMaterialIdList())) {
            throw new JeecgBootException("需要移库的物料信息不能为空");
        }
        WmsInReceiptTask inReceiptTask = inReceiptTaskService.getById(needMoveMaterialVO.getTaskId());
        if (Objects.isNull(inReceiptTask)) {
            throw new JeecgBootException("入库任务不存在");
        }
        if (!"R101".equals(inReceiptTask.getDocumentType())) {
            throw new JeecgBootException("入库任务单据类型错误");
        }
        WmsDeliveryTask deliveryTask = this.getOne(new LambdaQueryWrapper<WmsDeliveryTask>()
                .eq(WmsDeliveryTask::getInTaskCode, inReceiptTask.getDocumentCode())
        );
        if (Objects.isNull(deliveryTask)) {
            throw new JeecgBootException("调拨任务不存在");
        }
        WmsOutReceiptTask outReceiptTask = outReceiptTaskService.getOne(new LambdaQueryWrapper<WmsOutReceiptTask>()
                .eq(WmsOutReceiptTask::getDocumentCode, deliveryTask.getOutTaskCode()));
        if (Objects.isNull(outReceiptTask)) {
            throw new JeecgBootException("调拨出库任务不存在");
        }
        List<WmsInReceiptTaskMaterial> taskMaterialList = inReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .in(WmsInReceiptTaskMaterial::getId, needMoveMaterialVO.getTaskMaterialIdList())
        );
        if (CollectionUtil.isEmpty(taskMaterialList)) {
            throw new JeecgBootException("物料信息错误");
        }

        //构建移库单数据
        WmsMoveReceiptVO moveData = new WmsMoveReceiptVO();
        moveData.setSysOrgCode(inReceiptTask.getSysOrgCode());
        moveData.setOrganization(inReceiptTask.getOrganization());
        //组织内调拨
        moveData.setTransferType("1");
        moveData.setDocumentCode((String) FillRuleUtil.executeRule("yk_rule", null));
        moveData.setDocumentType("Y103");
        moveData.setOwnerType(inReceiptTask.getOwnerType());
        moveData.setBusinessDate(new Date());
        moveData.setTransferFromWarehouseId(deliveryTask.getInWarehouseId());
        moveData.setTransferFromOwner("默认");
        moveData.setWmsBasicWarehouseGroupId(null);
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        moveData.setWarehouseKeeper(loginUser.getUsername());
        moveData.setTransferFromDepartment("默认");
        moveData.setDocumentStatus(null);
        moveData.setDeliveryTaskId(deliveryTask.getId());
        moveData.setDeliveryInTaskId(inReceiptTask.getId());
        moveData.setDeliveryOutTaskId(outReceiptTask.getId());
        List<WmsMoveReceiptDetailVO> moveReceiptMaterialList = new ArrayList<>();
        for (WmsInReceiptTaskMaterial taskMaterial : taskMaterialList) {
            RepertorySimpleVO repertoryInfo = inReceiptTaskService.getMaterialRepertoryInfoByInId(taskMaterial.getId());
            if (!ObjectUtil.equal(repertoryInfo.getWmsBasicWarehouseId(),deliveryTask.getInWarehouseId())) {
                throw new JeecgBootException("调拨物料信息发生错误");
            }
            WmsMoveReceiptDetailVO moveMaterialData = new WmsMoveReceiptDetailVO();
            BeanUtil.copyProperties(taskMaterial, moveMaterialData);
            moveMaterialData.setId(null);
            moveMaterialData.setWmsBasicMaterialId(taskMaterial.getWmsBasicMaterialId());
            moveMaterialData.setSysOrgCode(taskMaterial.getSysOrgCode());
            moveMaterialData.setWmsBasicMaterialId(taskMaterial.getWmsBasicMaterialId());
            moveMaterialData.setBatchCode(taskMaterial.getBatchCode());
            moveMaterialData.setTransferQuantity(taskMaterial.getPlanQuantity());
            moveMaterialData.setTransferFromAreaId(repertoryInfo.getWmsBasicWarehouseAreaId());
            moveMaterialData.setTransferFromLocationId(repertoryInfo.getWmsBasicWarehouseAreaLocationId());
            moveMaterialData.setTransferToAreaId(taskMaterial.getSuggestedAreaId());
            moveMaterialData.setTransferToLocationId(taskMaterial.getSuggestedLocationId());
            moveMaterialData.setMaterialCode(repertoryInfo.getMaterialCode());
            moveMaterialData.setMaterialName(repertoryInfo.getMaterialName());
            moveMaterialData.setUpstreamDocuments("调拨入库任务");
            moveMaterialData.setUpstreamNumber(inReceiptTask.getDocumentCode());
            moveMaterialData.setUpstreamLineNumber(taskMaterial.getUpstreamLineNumber());
            moveMaterialData.setTaskMaterialId(taskMaterial.getId());
            moveReceiptMaterialList.add(moveMaterialData);
        }
        //根据code进行筛选
        if (!StrUtil.isEmpty(needMoveMaterialVO.getCode())) {
            moveReceiptMaterialList = moveReceiptMaterialList.stream().filter(o -> o.getBatchCode().equals(needMoveMaterialVO.getCode())).collect(Collectors.toList());
        }

        moveData.setWmsMoveReceiptMaterialList(moveReceiptMaterialList);
        return moveData;
    }

    /**
     * 条料调拨因出移库单-从出库任务中获取移库物料信息
     *
     * @param needMoveMaterialVO
     * @return
     */
    @Override
    public WmsMoveReceiptVO getMoveMaterialFormOutTask(NeedMoveMaterialVO needMoveMaterialVO) {
        if (CollectionUtil.isEmpty(needMoveMaterialVO.getTaskMaterialIdList())) {
            throw new JeecgBootException("需要移库的物料信息不能为空");
        }
        WmsOutReceiptTask outReceiptTask = outReceiptTaskService.getById(needMoveMaterialVO.getTaskId());
        if (Objects.isNull(outReceiptTask)) {
            throw new JeecgBootException("出库任务不存在");
        }
        if (!"C104".equals(outReceiptTask.getDocumentType())) {
            throw new JeecgBootException("出库任务单据类型错误");
        }
        WmsDeliveryTask deliveryTask = this.getOne(new LambdaQueryWrapper<WmsDeliveryTask>()
                .eq(WmsDeliveryTask::getOutTaskCode, outReceiptTask.getDocumentCode())
        );
        if (Objects.isNull(deliveryTask)) {
            throw new JeecgBootException("调拨任务不存在");
        }
        WmsInReceiptTask inReceiptTask = inReceiptTaskService.getOne(new LambdaQueryWrapper<WmsInReceiptTask>()
                .eq(WmsInReceiptTask::getDocumentCode, deliveryTask.getInTaskCode()));
        if (Objects.isNull(inReceiptTask)) {
            throw new JeecgBootException("调拨入库任务不存在");
        }
        List<WmsOutReceiptTaskMaterial> taskMaterialList = outReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
                .in(WmsOutReceiptTaskMaterial::getId, needMoveMaterialVO.getTaskMaterialIdList())
        );
        //校验物料是否已经移库
        boolean anyMatch = taskMaterialList.stream().anyMatch(taskMaterial -> ObjectUtil.equal(taskMaterial.getHasMoved(), 1));
        if (anyMatch) {
            throw new JeecgBootException("存在物料已经移库");
        }
        if (CollectionUtil.isEmpty(taskMaterialList)) {
            throw new JeecgBootException("物料信息错误");
        }

        //构建移库单数据
        WmsMoveReceiptVO moveData = new WmsMoveReceiptVO();
        moveData.setSysOrgCode(outReceiptTask.getSysOrgCode());
        moveData.setOrganization(outReceiptTask.getOrganization());
        //组织内调拨
        moveData.setTransferType("1");
        moveData.setDocumentCode((String) FillRuleUtil.executeRule("yk_rule", null));
        moveData.setDocumentType("Y106");
        moveData.setOwnerType(outReceiptTask.getOwnerType());
        moveData.setBusinessDate(new Date());
        moveData.setTransferFromWarehouseId(deliveryTask.getOutWarehouseId());
        moveData.setTransferFromOwner("默认");
        moveData.setWmsBasicWarehouseGroupId(null);
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        moveData.setWarehouseKeeper(loginUser.getUsername());
        moveData.setTransferFromDepartment("默认");
        moveData.setDocumentStatus(null);
        moveData.setDeliveryTaskId(deliveryTask.getId());
        moveData.setDeliveryInTaskId(inReceiptTask.getId());
        moveData.setDeliveryOutTaskId(outReceiptTask.getId());
        List<WmsMoveReceiptDetailVO> moveReceiptMaterialList = new ArrayList<>();
        for (WmsOutReceiptTaskMaterial taskMaterial : taskMaterialList) {
            RepertorySimpleVO repertoryInfo = outReceiptTaskService.getMaterialRepertoryInfoByOutId(taskMaterial.getId());
            if (!ObjectUtil.equal(repertoryInfo.getWmsBasicWarehouseId(), deliveryTask.getOutWarehouseId())) {
                throw new JeecgBootException("调拨物料信息发生错误");
            }
            WmsMoveReceiptDetailVO moveMaterialData = new WmsMoveReceiptDetailVO();
            BeanUtil.copyProperties(taskMaterial, moveMaterialData);
            moveMaterialData.setId(null);
            moveMaterialData.setSysOrgCode(taskMaterial.getSysOrgCode());
            moveMaterialData.setWmsBasicMaterialId(taskMaterial.getWmsBasicMaterialId());
            moveMaterialData.setBatchCode(taskMaterial.getBatchCode());
            moveMaterialData.setTransferQuantity(taskMaterial.getPlanQuantity());
            moveMaterialData.setTransferFromAreaId(repertoryInfo.getWmsBasicWarehouseAreaId());
            moveMaterialData.setTransferFromAreaName(repertoryInfo.getWmsBasicWarehouseAreaName());
            moveMaterialData.setTransferFromAreaCode(repertoryInfo.getWmsBasicWarehouseAreaCode());
            moveMaterialData.setTransferFromLocationId(repertoryInfo.getWmsBasicWarehouseAreaLocationId());
            moveMaterialData.setTransferFromLocationName(repertoryInfo.getWmsBasicWarehouseAreaLocationName());
            moveMaterialData.setTransferFromLocationCode(repertoryInfo.getWmsBasicWarehouseAreaLocationCode());
            moveMaterialData.setTransferToAreaId(null);
            moveMaterialData.setTransferToLocationId(null);
            moveMaterialData.setMaterialCode(repertoryInfo.getMaterialCode());
            moveMaterialData.setMaterialName(repertoryInfo.getMaterialName());
            moveMaterialData.setUpstreamDocuments("调拨出库任务");
            moveMaterialData.setUpstreamNumber(outReceiptTask.getDocumentCode());
            moveMaterialData.setUpstreamLineNumber(taskMaterial.getUpstreamLineNumber());
            moveMaterialData.setTaskMaterialId(taskMaterial.getId());
            moveReceiptMaterialList.add(moveMaterialData);
        }
        //根据code进行筛选
        if (!StrUtil.isEmpty(needMoveMaterialVO.getCode())) {
            moveReceiptMaterialList = moveReceiptMaterialList.stream().filter(o -> o.getBatchCode().equals(needMoveMaterialVO.getCode())).collect(Collectors.toList());
        }
        moveData.setWmsMoveReceiptMaterialList(moveReceiptMaterialList);
        return moveData;
    }

    /**
     * 条料调拨因出移库单-叫料架
     *
     * @param askMaterialRackVO
     */
    @Override
    public void askMaterialRack(AskMaterialRackVO askMaterialRackVO) {
        //组装叫料架数据
        MalledMaterialRackRequestVO requestVO = new MalledMaterialRackRequestVO();
        requestVO.setLineName(askMaterialRackVO.getLineName());
        requestVO.setPrepareArea(askMaterialRackVO.getPrepareArea());
        mdsDockingRepeaterService.malledMaterialRack(requestVO);
    }

    /**
     * 更新调拨任务过账状态
     *
     * @param simpleDocumentType
     * @param taskId
     * @param transferStatus
     */
    @Override
    public void updateTransferStatus(SimpleDocumentTypeEnum simpleDocumentType, String taskId, TransferStatusEnum transferStatus) {
        if (SimpleDocumentTypeEnum.DELIVERY_TASK == simpleDocumentType) {
            if (StrUtil.isEmpty(taskId)) {
                return;
            }
            this.update(new LambdaUpdateWrapper<WmsDeliveryTask>()
                    .set(WmsDeliveryTask::getTransferSapStatus, transferStatus.getCode())
                    .eq(WmsDeliveryTask::getId, taskId)
            );
        } else {
            log.error("不支持的类型{}", simpleDocumentType);
        }
    }

    /**
     * 调拨任务过账
     *
     * @param taskId
     * @return
     */
    @Override
    public List<String> deliveryTaskTransfer(String taskId) {
        List<String> errorList = new ArrayList<>();
        if (StrUtil.isEmpty(taskId)) {
            return errorList;
        }
        WmsDeliveryTask wmsDeliveryTask = this.getById(taskId);
        if (Objects.isNull(wmsDeliveryTask)) {
            throw new JeecgBootException("调拨任务不存在");
        }
        //调用第三方接口
        List<SapBaseResponseDTO> sapBaseResponseDTOList=new ArrayList<>();
        try {
            sapBaseResponseDTOList = sapOperateService.deliveryByTask(taskId);
        } catch (Exception e) {
            log.error(e.getMessage());
            errorList.add(e.getMessage());
            //更新过账状态 过账失败
            updateTransferStatus(SimpleDocumentTypeEnum.DELIVERY_TASK, taskId,  TransferStatusEnum.TRANSFER_FAIL);
        }
        if (!CollectionUtil.isEmpty(sapBaseResponseDTOList)) {
            for (SapBaseResponseDTO sapBaseResponseDTO: sapBaseResponseDTOList) {
                if ("E".equals(sapBaseResponseDTO.getResult())) {
                    errorList.add(sapBaseResponseDTO.getMessage());
                    //更新过账状态 过账失败
                    updateTransferStatus(SimpleDocumentTypeEnum.DELIVERY_TASK, taskId,  TransferStatusEnum.TRANSFER_FAIL);
                }
            }
        }
        if (CollectionUtil.isEmpty(errorList)) {
            updateTransferStatus(SimpleDocumentTypeEnum.DELIVERY_TASK, taskId, TransferStatusEnum.TRANSFER_SUCCESS);
        }
        return errorList;
    }

}
